package com.pishou.code.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pishou.URLConfig;
import com.pishou.code.bean.Video;
import com.pishou.code.util.HttpPostClient;
import com.pishou.ParamConfig;
import com.pishou.code.util.VideoPlayList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class UpdatePlayList extends Thread {
	
	private Integer playerId;
	
	public UpdatePlayList(Integer playerId) {
		super();
		this.playerId = playerId;
	}

	
	@Override
	public void run() {
		try {
			update();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.run();
	}

	public void update() {
		String param = "id="+ this.playerId;
		String re = HttpPostClient.sendPost(URLConfig.url_playList, param);
		JSONObject result_jsoon = JSONObject.parseObject(re);
		if(result_jsoon.getInteger("success").equals(1)) {
			if(result_jsoon.getJSONArray("entity").size()>0) {
				JSONArray playList_array = result_jsoon.getJSONArray("entity");
				List<Video> payList = new ArrayList<>();
				Iterator it = playList_array.iterator();
				while (it.hasNext()) {
					JSONObject json = (JSONObject) it.next();
					Video v = new Video(json.getString("name"), ParamConfig.file_path+ "/video/AD" +getFileName(json.getString("oss")) , json.getString("oss"),json.getInteger("commentId"),json.getString("content"),json.getInteger("pictures"),json.getLong("sku"),json.getInteger("rotation"));
					payList.add(v);
				}
				//payList.add(new Video("广告片", "C:\\videoplayer\\video\\add.mp4", "VideoResources/add.mp4"));
				// 保存到静态变量和CSV中
				VideoPlayList.setVideo_list(payList);
			}
		}
	}
	
	private String getFileName(String oss) {
		oss = oss.trim();
		if(oss.indexOf("/")>-1) {
			return oss.substring(oss.lastIndexOf("/")+1);
		}else if(oss.indexOf("\\")>-1) {
			return oss.substring(oss.lastIndexOf("\\")+1);
		}
		return "";
	}

}
